package com.wjc.train.business.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjc.train.business.entity.DailyTrainCarriage;
import com.wjc.train.business.entity.TrainCarriage;
import com.wjc.train.business.enums.SeatColEnum;
import com.wjc.train.business.repository.DailyTrainCarriageRepository;
import com.wjc.train.business.repository.TrainCarriageRepository;
import com.wjc.train.business.req.DailyTrainCarriageQueryReq;
import com.wjc.train.business.req.DailyTrainCarriageSaveReq;
import com.wjc.train.business.resp.DailyTrainCarriageQueryResp;
import com.wjc.train.business.service.IDailyTrainCarriageService;
import com.wjc.train.common.enums.LogicEnum;
import com.wjc.train.common.untils.CopyUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 每日车厢表 服务实现类
 * </p>
 *
 * @author Guessed
 * @since 2023-08-08
 */
@Service
@Slf4j
public class DailyTrainCarriageServiceImpl  implements IDailyTrainCarriageService {

    @Resource
    private DailyTrainCarriageRepository dailytraincarriageRepository;

    @Resource
    private TrainCarriageRepository traincarriageRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DailyTrainCarriageSaveReq dailytraincarriageSaveReq) {
        DailyTrainCarriage dailytraincarriage = CopyUtil.copy(dailytraincarriageSaveReq, DailyTrainCarriage.class);
        //计算座位数
        int colSize = SeatColEnum.getColsByType(dailytraincarriage.getSeatType()).size();
        dailytraincarriage.setColumnCount(colSize);
        dailytraincarriage.setSeatCount(colSize * dailytraincarriage.getRowCount());
        dailytraincarriageRepository.saveOrUpdate(dailytraincarriage);
    }

    @Override
    public IPage<DailyTrainCarriageQueryResp> queryList(IPage<DailyTrainCarriageQueryResp> page, DailyTrainCarriageQueryReq dailytraincarriageQueryReq) {
        return dailytraincarriageRepository.queryList(page,dailytraincarriageQueryReq);
    }

    @Override
    public void delete(Long id) {
        dailytraincarriageRepository.getBaseMapper().deleteById(id);
    }

    @Override
    public void genDaily(Date date, String trainCode) {
        log.info("开始生成日期:{},车次:{}的车厢信息", DateUtil.formatDate(date),trainCode);
        //查询当日车厢信息并删除
        List<DailyTrainCarriage> dailyTrainCarriageList =
                dailytraincarriageRepository.lambdaQuery()
                .eq(DailyTrainCarriage::getDate, date)
                .eq(DailyTrainCarriage::getIsDeleted, LogicEnum.NOT_DELETED.getCode())
                .eq(DailyTrainCarriage::getTrainCode, trainCode).list();

        if(CollectionUtil.isNotEmpty(dailyTrainCarriageList)){
            List<Long> collect = dailyTrainCarriageList.stream().map(DailyTrainCarriage::getId).collect(Collectors.toList());
            dailytraincarriageRepository.getBaseMapper().deleteBatchIds(collect);
        }
        //查询该车次的所有车厢信息
        List<TrainCarriage> trainCarriages = traincarriageRepository.lambdaQuery()
                .eq(TrainCarriage::getTrainCode, trainCode)
                .eq(TrainCarriage::getIsDeleted, LogicEnum.NOT_DELETED.getCode()).list();
        if(CollectionUtil.isEmpty(trainCarriages)){
            log.error("该车次不存在车厢信息，车次编号为:{}",trainCode);
            return;
        }
        //生成每日车厢信息

        List<DailyTrainCarriage> trainCarriageList =
                CopyUtil.copyList(trainCarriages, DailyTrainCarriage.class);

        trainCarriageList.forEach(x->{
            x.setId(null);
            x.setDate(date);
        });
        dailytraincarriageRepository.saveBatch(trainCarriageList);
        log.info("结束生成日期:{},车次:{}的车厢信息", DateUtil.formatDate(date),trainCode);
    }
}
